package gonggong;

import java.sql.SQLException;
import java.sql.Statement;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

import util.Constant;
import util.TotalClass;

import com.SmsModule.bean.TbSmsSend;
import com.SmsModule.dao.TbSmsSendDAO;
import com.dati.bean.Dati;
import com.dati.bean.DatiMiddle;
import com.dati.bean.SendDati;
import com.dati.bean.Xulie;
import com.dati.dao.DatiDao;
import com.dati.dao.DatiTypeDao;
import com.order.bean.TbOrder;
import com.order.dao.TbOrderDao;

public class ScanDatiDao {

	/**
	 *@author qingyu zhang
	 *@function:
	 *2011-4-13
	 *上午10:36:07
	 *zzClub
	 *gonggong
	 */
	Statement st = null;
	public void operator(){
		List<Dati> datiList = new ScanDatiDao().getDatiList();
		if(datiList!=null&&datiList.size()>0){
			new ScanDatiDao().sendDati(datiList);
			boolean bo = modifyState(datiList);
		}
	}
	/**
	 * 
	 *@author qingyu zhang
	 *@function:
	 * @return
	 *2011-4-13
	 *上午10:46:36
	 *zzClub
	 *gonggong
	 *List<Dati>
	 */
	public List<Dati> getDatiList(){
		String condition="state=0";
		List<Dati> datiList = new DatiDao().getDatiList("tb_send_dati", "", condition);
		return datiList;
	}
	public void sendDati(List<Dati> datiList){
		if(datiList!=null&&datiList.size()>0){
			for(Dati dati:datiList){
				String phone = dati.getSendPhone();
				String content = dati.getSmscontent();
				String lastContent = content;//content.substring(2).trim().toLowerCase();//答题指令如：生活(sh)
				System.out.println("指令=="+lastContent);
				getDatiType(lastContent,phone);
			}
		}
	}
	/**
	 * 
	 *@author qingyu zhang
	 *@function:根据指令值得到与其相应的答题 ID；
	 * @param orderName
	 *2011-4-13
	 *上午11:09:45
	 *zzClub
	 *gonggong
	 *void
	 */
	public void getDatiType(String orderName,String phone){
		String condition="orderValue='"+orderName+"' and orderType=1";
		List<TbOrder> orderList = new TbOrderDao().getOrderList("tb_order", "createTime", condition);
		TbOrder order = orderList.get(0);
		String typeId = order.getTypeId();
		getXilieList(typeId,phone);
	}
	/**
	 * 
	 *@author qingyu zhang
	 *@function:根据答题类型得到其下的所有系列
	 * @param typeId
	 * @return
	 *2011-4-13
	 *上午11:15:23
	 *zzClub
	 *gonggong
	 *List<Xulie>
	 */
	public void  getXilieList(String typeId,String phone){
		String condition="answerTypeId='"+typeId+"'";
		List<Xulie> xilieList = new DatiTypeDao().getXulieByTypeId("tb_serise", "createTime", condition);
		int num = 0;
		Xulie xilie = null;
		/**
		 * 随机从所有系列中选择一个系列
		 */
		if(xilieList!=null&&xilieList.size()>0){
			num = new Random().nextInt(xilieList.size());
			xilie = xilieList.get(num);
		}
		if(xilie!=null){
			getTimu(xilie,phone);
		}
	}
	public void getTimu(Xulie xilie,String phone){
		List<TbSmsSend> smsSendList = new ArrayList<TbSmsSend>();
		String condition = "seriseId='"+xilie.getSeriseId()+"'";
		List<DatiMiddle> datiMiddle = new DatiDao().getDatiMiddle("tb_answer_middle", "createTime", condition);
		if(datiMiddle!=null&&datiMiddle.size()>0){
			 List<SendDati> sendDatiList = this.saveTimu(datiMiddle,phone);
			 //保存到表中
			 if(sendDatiList!=null&&sendDatiList.size()>0){
				 boolean bo = saveSmsTosendDati(sendDatiList);
				 if(bo){
					 TbSmsSend smsSend = new TbSmsSend();
					 SendDati sendDati = sendDatiList.get(0);
					 smsSend.setCreateTime(Constant.decodeDate());
					 smsSend.setDestAdd(sendDati.getPhone());
					 smsSend.setIsSendTime(0);
					 smsSend.setSendTime(Constant.decodeDate());
					 String timuId = sendDati.getAnswerMiddleId();
					 String condi="answerMiddleId='"+timuId+"'";
					 List<DatiMiddle> datiMi = new DatiDao().getDatiMiddle("tb_answer_middle", "createTime", condi);
					 smsSend.setSmsContent(datiMi.get(0).getAnswerContent());
					 smsSend.setSmsSendState(0);
					 smsSend.setUserId(0);
					 smsSend.setSeriseId("0"+sendDati.getSeriseId()+sendDati.getAnswerMiddleId());
					 smsSendList.add(smsSend);
					 boolean boo = new TbSmsSendDAO().addSmsListDati(smsSendList);
					 if(boo){
						 boolean booo = new DatiDao().modifySendDati(sendDati.getAnswerMiddleId(),phone);
						 System.out.println("修改成功!");
					 }
				 }
			 }
		}
		
	}
	/**
	 * 
	 *@author qingyu zhang
	 *@function:存放要发送答题到表sendDati
	 * @param datiMiddleList
	 *2011-4-13
	 *上午11:35:05
	 *zzClub
	 *gonggong
	 *void
	 */
	public  List<SendDati> saveTimu(List<DatiMiddle> datiMiddleList,String phone){
		  List<SendDati> sendDatiList = new ArrayList<SendDati>();
		  Format format=new SimpleDateFormat("yyyy-MM-dd HH:mm");
		  Date today=new Date();
		  long time=0;
		  int fen = Integer.parseInt(new DatiDao().getSetTime());
		  time=(today.getTime()/1000)+60*fen;
		  Date newDate=new Date();
		  newDate.setTime(time*1000); 
		for(DatiMiddle datiMiddle:datiMiddleList){
			SendDati sendDati = new SendDati();
			sendDati.setAnswerMiddleId(datiMiddle.getAnswerMiddleId());
			sendDati.setCreateTime(format.format(today));
			sendDati.setEndTime(format.format(newDate));
			sendDati.setPhone(phone);
			sendDati.setRightOrno(9);
			sendDati.setState(0);
			sendDati.setSeriseId(datiMiddle.getSeriseId());
			sendDatiList.add(sendDati);
		}
		return sendDatiList;
	}
	/**
	 * 
	 *@author qingyu zhang
	 *@function:把得到的系列答题存放 到表sendDati中
	 * @param sendDatiList
	 * @return
	 *2011-4-13
	 *下午01:23:51
	 *zzClub
	 *gonggong
	 *boolean
	 */
	public boolean saveSmsTosendDati(List<SendDati> sendDatiList){
		boolean bo = false;
		st = new TotalClass().getStatement();
		try {
		for(SendDati sendDati:sendDatiList){
				String hql ="insert into sendDati (seriseId,answerMiddleId,state,phone,createTime,endTime,rightOrno) " +
				"values('"+sendDati.getSeriseId()+"','"+sendDati.getAnswerMiddleId()+"',"+sendDati.getState()+",'"+sendDati.getPhone()
				+"','"+sendDati.getCreateTime()+"','"+sendDati.getEndTime()+"',"+sendDati.getRightOrno()+")";
				System.out.println("导入表sendDati==="+hql);
				st.addBatch(hql);
			}
			st.executeBatch();
			bo = true;
		  } catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return bo ;
	}
	/**
	 * 修改表sendDati中的state值
	 *@author qingyu zhang
	 *@function:
	 * @param dati
	 * @return
	 *2011-4-13
	 *下午01:43:54
	 *zzClub
	 *gonggong
	 *boolean
	 */
	public boolean modifyState(List<Dati> datiList){
		boolean bo = false;
		String datiId = "";
		for(Dati dati:datiList){
			if(datiId.equals("")){
				datiId+=dati.getDatiId();
			}else{
				datiId+=","+dati.getDatiId();
			}
		}
		bo = new DatiDao().modifyDati(datiId);
		return bo ;
	}
}
